package com.apes.framework.plugin.outbox;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName OutboxSubscriberService
 * @Description TODO
 * @Author pengyb
 * @Date 2019/10/24 16:57
 * @Version 1.0
 **/
@Service("outboxSubscriberService")
public class OutboxSubscriberService extends DomainService {

    @Autowired
    private OutboxSubscriberRepository outboxSubscriberRepository;

    @Autowired
    private OutboxSubscriberManager outboxSubscriberManager;

    public OutboxSubscriber save(SimpleRequest request) {
        OutboxSubscriber subscriber = request.getO(OutboxSubscriber.class);
        if (subscriber.isReadonly()) {
            return subscriber;
        }
        if (null != subscriber.getAggregateType() && subscriber.getAggregateType().contains("{")) {
            subscriber.setAggregateType((String) JSONObject.parseObject(subscriber.getAggregateType()).get("id"));
        }
        if (null != subscriber.getConsumer() && subscriber.getConsumer().contains("{")) {
            subscriber.setConsumer((String) JSONObject.parseObject(subscriber.getConsumer()).get("id"));
        }
        subscriber = outboxSubscriberRepository.saveAndFlush(subscriber);
        outboxSubscriberManager.addOutboxSubscriber(subscriber);
        return subscriber;
    }

    public String delete(SimpleRequest request) {
        OutboxSubscriber subscriber = request.getO(OutboxSubscriber.class);
        if (subscriber.isReadonly()) {
            return null;
        }
        outboxSubscriberRepository.delete(subscriber);
        outboxSubscriberManager.removeOutboxSubscriber(subscriber);
        return "删除成功";
    }

    @Listener(topic = "outbox.subscriber.init")
    public List<OutboxSubscriber> findAllOutboxSubscribers(SimpleRequest request) {
        return outboxSubscriberRepository.findAll();
    }

    public Page<OutboxSubscriber> findAll(SimpleRequest request) {
        List<OutboxSubscriber> outboxSubscribers = outboxSubscriberManager.getAllOutboxSubscribers();
        return (Page<OutboxSubscriber>) paged(outboxSubscribers, request.get("page"), request.get("size"), null);
    }

    /**
     * 公用请求
     * @param subscriber
     * @param aggregateId
     * @param object
     */
    public void commonSendRequest(String subscriber, String aggregateId, JSONObject payload) {
        this.commonSendRequest(subscriber, aggregateId, payload.toJSONString());
    }

    public void commonSendRequest(String subscriber, String aggregateId, Map payload) {
        this.commonSendRequest(subscriber, aggregateId, JSONObject.toJSONString(payload));
    }

    public void commonSendRequest(String subscriber, String aggregateId, String payload) {
        OutboxPayloadWrapper outboxPayloadWrapper = new OutboxPayloadWrapper();
        outboxPayloadWrapper.setSubscriber(subscriber);
        outboxPayloadWrapper.setAggregateId(aggregateId);
        try {
            outboxPayloadWrapper.setPayload(payload);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        SpringManager.getBean(OutboxEventService.class).sendSaveEvent(outboxPayloadWrapper);
    }
}
